package com.video.Controller;

import com.video.entity.User;
import com.video.service.UserService;
import com.videos.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static com.videos.utils.MailUtils.getValidateCode;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("loginUser")
    @ResponseBody
    public String loginUser(User user, HttpServletRequest request) {
        User loginUser = userService.loginUser(user);
        if (loginUser != null) {
            request.getSession().setAttribute("user", loginUser);
            return "success";
        }
        return "啊~~~~~";
    }

    @RequestMapping("showMyProfile")
    public String showMyProfile(Model model, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("user");
        User user = userService.findById(loginUser.getId());
        model.addAttribute("user", user);
        request.getSession().setAttribute("user", user);
        return "/before/my_profile.jsp";
    }


    @RequestMapping("changeProfile")
    public String changeProfile() {
        return "/before/change_profile.jsp";
    }

    //updateUser
    @RequestMapping("updateUser")
    public String updateUser(User user, HttpSession session) {
        User userServiceById = userService.findById(user.getId());
        userServiceById.setNickname(user.getNickname());
        userServiceById.setSex(user.getSex());
        userServiceById.setBirthday(user.getBirthday());
        userServiceById.setAddress(user.getAddress());
        userService.updateUser(userServiceById);
        session.setAttribute("user", userServiceById);
        return "redirect:/user/showMyProfile";
    }


    @RequestMapping("changeAvatar")
    public String changeAvatar() {
        return "/before/change_avatar.jsp";
    }

    @RequestMapping("upLoadImage")
    public String upLoadImage(MultipartFile image_file, Model model, HttpSession session) {
        //上传文件路径
        String path = "H:\\apache-tomcat-8.5.41\\webapps\\upload\\";
        //判断该路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        //获取上传文件的名称
        String filename = image_file.getOriginalFilename();
        System.out.println(filename);
        // 把文件的名称设置唯一值，uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename;
        // 完成文件上传
        File file1 = new File(path, filename);
        try {
            image_file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        User user = (User) session.getAttribute("user");
        User serviceById = userService.findById(user.getId());
        serviceById.setImgurl(filename);
        userService.updateUser(serviceById);
        session.setAttribute("user", serviceById);

        return "redirect:/user/showMyProfile";
    }

    @RequestMapping("passwordSafe")
    public String passwordSafe() {
        return "/before/password_safe.jsp";
    }

    @RequestMapping("validatePassword")
    @ResponseBody
    public String validatePassword(String password, HttpSession session) {
        System.out.println(password);
        User user = (User) session.getAttribute("user");
        System.out.println(user.getPassword());
        if (password.equals(user.getPassword())) {
            return "success";
        }
        return "哦~~~";
    }

    @RequestMapping("updatePassword")
    public String updatePassword(String newPassword, HttpSession session) {
        System.out.println(newPassword);
        User user = (User) session.getAttribute("user");
        user.setPassword(newPassword);
        userService.updateUser(user);
        return "redirect:/user/showMyProfile";
    }

    @RequestMapping("loginOut2")
    public String loginOut2(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/subject/selectAll";
    }

    //loginOut
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/subject/selectAll";
    }

    //validateEmail
    @RequestMapping("validateEmail")
    @ResponseBody
    public String validateEmail(String email, HttpSession session) {
        User user = userService.validateEmail(email);
        if(user == null){
            return "success";
        }
       return null;
    }

    @RequestMapping("insertUser")
    @ResponseBody
    public String insertUser(User user, HttpSession session) {
        userService.insertUser(user);
        session.setAttribute("user", user);
        System.out.println(user);
        return "success";
    }

    //forgetPassword

    @RequestMapping("forgetPassword")
    public String forgetPassword() {
//        User user = (User) session.getAttribute("user");
//        String email = user.getEmail();
//        model.addAttribute("email",email);
        return "/before/forget_password.jsp";
    }


    @RequestMapping("sendEmail")
    @ResponseBody
    public String sendEmail(String email, HttpSession session) {
        User user = userService.validateEmail(email);
        if (user == null) {
            return "hasNoUser";
        } else {
            String validateCode = getValidateCode(6);
            boolean b = MailUtils.sendMail("853829609@qq.com", "你好，这是一封测试邮件，无需回复。",
                    "测试邮件随机生成的验证码是：" + validateCode);
            if(b){
                session.setAttribute("validateCode",validateCode);
                return "success";
            }
           return null;
        }
    }

    @RequestMapping("validateEmailCode")
    public String validateEmailCode(String email,String code,Model model,HttpSession session) {
        String  validateCode = (String) session.getAttribute("validateCode");
        if(code.equals(validateCode)){
            model.addAttribute("email",email);
            return "/before/reset_password.jsp";
        }
        model.addAttribute("msg","验证码错误");
        return "/before/forget_password.jsp";
    }

    @RequestMapping("resetPassword")
    public String resetPassword(String email,String password){
        System.out.println(password + email);
        User user = userService.validateEmail(email);
        user.setPassword(password);
        userService.updateUser(user);
        return "/before/index.jsp";
    }
}

